package com.xinlu.user.action;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.xinlu.common.exception.CommonException;
import com.xinlu.common.log.Logger;
import com.xinlu.derek.common.BaseAction;
import com.xinlu.persistence.Users;
import com.xinlu.user.bo.DlrzBo;
import com.xinlu.user.bo.UsersBo;
import com.xinlu.user.bo.XxtxBo;
import com.xinlu.user.dao.MD5Util;
import com.xinlu.user.form.UserLoginForm;
import com.xinlu.wzpz.bo.WzpzBo;

public class UserLoginAction extends BaseAction {

	Logger log = Logger.getLogger(UserLoginAction.class);

	private UsersBo usersBo;
	private WzpzBo wzpzBo;
	private DlrzBo dlrzBo;
	private XxtxBo xxtxBo;

	public void setWzpzBo(WzpzBo wzpzBo) {
		this.wzpzBo = wzpzBo;
	}

	public void setUsersBo(UsersBo usersBo) {
		this.usersBo = usersBo;
	}

	public void setDlrzBo(DlrzBo dlrzBo) {
		this.dlrzBo = dlrzBo;
	}

	public void setXxtxBo(XxtxBo xxtxBo) {
		this.xxtxBo = xxtxBo;
	}

	/** form 的 初始化 * */
	private UserLoginForm fr = new UserLoginForm();

	public UserLoginForm getModel() {

		return fr;
	}

	// ------- Business Object 注入开始-------

	// ------- Business Object 注入结束 -------

	public String doExecute() throws CommonException {

		return SUCCESS;
	}

	public String checkUser() throws NoSuchAlgorithmException, IOException {

		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpSession session = request.getSession();

		Users user = usersBo.getUserInfo(fr.getUsername(), MD5Util.encodePassword(fr.getPwd()));

		// log.info(user.getUsername());

		String rolename = null;
		String successFlag = null;
		if (user != null) {

			if (user.getLock() == 0) {

				if (user.getDaili() > 0) {

					rolename = "agent";
				} else {
					rolename = "member";
				}

				successFlag = "success";

				String[] mss = user.getMss().split(",");

				session.setAttribute("loginFlag", successFlag);
				session.setAttribute("rolename", rolename);
				session.setAttribute("userinfo", user);
				session.setAttribute("mss", mss);
				session.setAttribute("xxtx", xxtxBo.loadXxtx(user.getUsername()));

				// 网站配置session
				session.setAttribute("wzpz", wzpzBo.loadWzpz());

				String brower = request.getHeader("User-Agent");

				this.dlrzBo.addDlrzInfo(brower, this.getIpAddr(request), user.getUsername());

			} else if (user.getLock() == 1) {

				successFlag = "locked";
			}
		} else {
			rolename = null;
			successFlag = "fail";
		}

		response.getWriter().write(successFlag);

		return null;
	}

	public String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}

	/**
	 * 
	 * Function : 退出账号
	 * 
	 * @return Derek 2011-5-24 下午09:43:25
	 */
	public String loginOut() {

		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();

		session.invalidate();

		return null;

	}
	
	public String getLastedInfo() throws IOException{
		
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpSession session = request.getSession();
		
		Users userinfo = (Users) session.getAttribute("userinfo");
		
		String data = null;
		
		if (userinfo != null) {
			
			Users newUses = usersBo.loadUser(userinfo.getUsername());
			
			session.setAttribute("userinfo", newUses);
			
			data = "" + newUses.getUermoney();
		}
		
		logger.info("用户最新的钱为：" + data);
		
		
		response.getWriter().write("" + data);
		
		return null;
	}

	public void formInit() throws CommonException {

	}

}
